import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:sptapp/firebase_options.dart';
import 'package:sptapp/routes/route.dart';
import 'package:sptapp/splash/splash.dart';

import 'basic/base_info.dart';
import 'config/const.dart';
import 'moudle/message.dart';
import 'firebase_options.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations(
    [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
  );
  HttpOverrides.global = OverriedHttpOverrides();
  // if (Platform.isAndroid) {
// 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
  SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
    statusBarColor: Colors.black,
    systemNavigationBarColor: Colors.black,
    statusBarBrightness: Brightness.dark,
  );
  SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  // }
  Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform
  ).then((val){
    LocalBaseInfo.init().then((res) {
      runApp(const MyApp());
    });
  });
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(
        750,
        1624,
      ),
      builder: (_, child){
        return GetMaterialApp(
          locale:  Locale("en","US"),//Get.deviceLocale,
          translations: Message(),
          fallbackLocale: Locale("en","US"), //Locale("zh","CN"),
          debugShowCheckedModeBanner: false,
          title: "SPT APP",
          theme: ThemeData(
            brightness: Brightness.light,
            textSelectionTheme: const TextSelectionThemeData(
              cursorColor: themeColor, // 设置光标颜色为红色
            ),
          ),
          home: const SplashPage(),
          onGenerateRoute: MyRoute().onGenerateRoute,
          navigatorKey: MyRoute.navigatorKey,
          builder: (context,widget){
            return MediaQuery(data: MediaQuery.of(context).copyWith(textScaler: TextScaler.noScaling,boldText: false), child: FlutterEasyLoading(
                child: FlutterSmartDialog(
                  child: widget,
                )
            ));
          },
        );
          MaterialApp(
          color: themeColor,
          theme: ThemeData(
            textSelectionTheme: const TextSelectionThemeData(
              cursorColor: themeColor, // 设置光标颜色为红色
            ),
          ),
          home: const SplashPage(),
          onGenerateRoute: MyRoute().onGenerateRoute,
          navigatorKey: MyRoute.navigatorKey,
          builder: (context,widget){
            return FlutterEasyLoading(
                child: FlutterSmartDialog(
                  child: widget,
                )
            );
          },
        );
      },
    );
  }
}


class OverriedHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}
